package com.gxkj.movie.utils.logback;

import ch.qos.logback.classic.pattern.ClassicConverter;
import ch.qos.logback.classic.spi.ILoggingEvent;

import java.net.Inet4Address;
import java.net.InetAddress;
import java.net.NetworkInterface;
import java.net.SocketException;
import java.util.Enumeration;

public class IPLogConvert extends ClassicConverter {
    private static String localIp;

    public IPLogConvert() {
    }

    @Override
    public String convert(ILoggingEvent iLoggingEvent) {
        if (localIp == null) {
            try {
                Enumeration allNetInterfaces = NetworkInterface.getNetworkInterfaces();

                while(allNetInterfaces.hasMoreElements()) {
                    NetworkInterface netInterface = (NetworkInterface)allNetInterfaces.nextElement();
                    Enumeration addresses = netInterface.getInetAddresses();

                    while(addresses.hasMoreElements()) {
                        InetAddress ip = (InetAddress)addresses.nextElement();
                        if (ip != null && ip instanceof Inet4Address && !ip.isLoopbackAddress() && ip.getHostAddress().indexOf(":") == -1) {
                            localIp = ip.getHostAddress();
                        }
                    }
                }
            } catch (SocketException var6) {
                ;
            } catch (Exception var7) {
                ;
            }
        }

        return localIp;
    }
}